package com.ruoyi.web.controller.material;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.material.domain.SysSwitch;
import com.ruoyi.material.service.ISysSwitchService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.material.domain.SysMaterial;
import com.ruoyi.material.service.ISysMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 上传材料Controller
 * 
 * @author hsx
 * @date 2022-01-07
 */
@RestController
@RequestMapping("/material/upload")
public class SysMaterialController extends BaseController
{
    @Autowired
    private ISysMaterialService sysMaterialService;
    
    @Autowired
    private ISysSwitchService sysSwitchService;

    /**
     * 查询上传材料列表
     */
    @PreAuthorize("@ss.hasPermi('material:upload:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMaterial sysMaterial)
    {
        // 查询开关状态
        Integer showResult = sysSwitchService.selectSysSwitchById(1).getShowResult();
        sysMaterial.setShowResult(showResult==1);
        startPage();
        List<SysMaterial> list = sysMaterialService.selectSysMaterialList(sysMaterial);
        return getDataTable(list);
    }

    /**
     * 导出上传材料列表
     */
    @PreAuthorize("@ss.hasPermi('material:upload:export')")
    @Log(title = "上传材料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMaterial sysMaterial)
    {
        List<SysMaterial> list = sysMaterialService.selectSysMaterialList(sysMaterial);
        ExcelUtil<SysMaterial> util = new ExcelUtil<SysMaterial>(SysMaterial.class);
        util.exportExcel(response, list, "上传材料数据");
    }

    /**
     * 获取上传材料详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:upload:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        return AjaxResult.success(sysMaterialService.selectSysMaterialByMaterialId(materialId));
    }

    /**
     * 新增上传材料
     */
    @PreAuthorize("@ss.hasPermi('material:upload:add')")
    @Log(title = "上传材料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMaterial sysMaterial)
    {
        return toAjax(sysMaterialService.insertSysMaterial(sysMaterial));
    }

    /**
     * 修改上传材料
     */
    @PreAuthorize("@ss.hasPermi('material:upload:edit')")
    @Log(title = "上传材料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMaterial sysMaterial)
    {
        return toAjax(sysMaterialService.updateSysMaterial(sysMaterial));
    }

    /**
     * 删除上传材料
     */
    @PreAuthorize("@ss.hasPermi('material:upload:remove')")
    @Log(title = "上传材料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable Long[] materialIds)
    {
        return toAjax(sysMaterialService.deleteSysMaterialByMaterialIds(materialIds));
    }

    /**
     * 查询显示结果开关
     */
    @GetMapping( "/getShowResult")
    public AjaxResult getShowResult()
    {
        return AjaxResult.success(sysSwitchService.selectSysSwitchById(1).getShowResult());
    }

    /**
     * 修改显示结果开关
     */
    @PutMapping( "/updateShowResult/{showResult}")
    public AjaxResult updateShowResult(@PathVariable("showResult") Boolean showResult)
    {
        SysSwitch sysSwitch = new SysSwitch();
        sysSwitch.setId(1);
        sysSwitch.setShowResult(showResult? 1:0);
        // 更新开关状态
        return AjaxResult.success(sysSwitchService.updateSysSwitch(sysSwitch));
    }
}
